package com.scenic.rd_android.basic_knowledge;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nova.scenic.projectlibs.util.DebugUtil;
import com.nova.scenic.projectlibs.util.Observer;
import com.scenic.rd_android.BaseActivity;
import com.scenic.rd_android.R;

import java.util.List;

/**
 * Created by scenic on 16/9/3.
 */
public class ActivityTaskTest extends BaseActivity {

    static int idCreate = 0;
    {
        idCreate++;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_activity_task_test);
        TextView textView = (TextView) findViewById(R.id.id_textView_showID);
        textView.setText("ID " + idCreate);

        DebugUtil.getInstance().addCadConsumer(new Observer<DebugUtil>() {
            @Override
            public void update(com.nova.scenic.projectlibs.util.Observable observable, DebugUtil data) {
                switch (data.nextCmdValueAsInt()) {

                    case 0:
                        //通过  adb shell dumpsys activity activities 查看
                        showTaskInfo(data);
                        break;
                    case 1:
                        break;
                }
            }
        });

    }


    public void showTaskInfo(DebugUtil util) {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
        log("tasks length is " + tasks.size());

        for (int i = 0; i < tasks.size(); i++) {

            ActivityManager.RecentTaskInfo taskInfo = tasks.get(i).getTaskInfo();
            StringBuilder builder = new StringBuilder();
            builder.append("task id " + i).append("\t");
            builder.append(taskInfo.baseActivity.getShortClassName()).append("\t");
            builder.append(taskInfo.topActivity.getShortClassName()).append("\t");

            util.print(builder.toString());
            log(builder.toString());
            log("\n");
        }
    }

    public void testStandard(View view) {

        startActivity(new Intent(this, Activity1.class));

    }

    /**
     * 当系统接收到一个Intent实例去启动一个singleTop模式的Activity时，
     * 如果在目标栈的顶部存在一个该Activity的实例的话，
     * 那么系统就会重用这个Activity的实例而不创建新的实例，
     * 并回调该Activity的onNewIntent(Intent intent)
     * 方法把新的Intent实例当作方法参数传递进去；
     * 如果在目标栈的顶部没有该Activity的实例的话系统将会在新建一个Activity实例，
     * 与standard的行为就一样了。
     *
     * @param view
     */
    public void testSingleTopTop(View view) {
        startActivity(new Intent(this, Activity2.class));
    }

    public void testSingleTask(View view) {
        startActivity(new Intent(this, Activity3.class));
    }

    public void testSingleInstance(View view) {
        startActivity(new Intent(this, Activity4.class));
    }


    public static class Activity1 extends BaseActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Button button = new Button(this);
            button.setText("Activity1");
            setContentView(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getApplicationContext(), ActivityTaskTest.class));
                }
            });
        }
    }

    public static class Activity2 extends BaseActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Button button = new Button(this);
            button.setText("Activity2_Single_top");
            setContentView(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), ActivityTaskTest.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                }
            });
        }

    }

    public static class Activity3 extends BaseActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Button button = new Button(this);
            button.setText("Activity2_Single_Task");
            setContentView(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), ActivityTaskTest.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            });
        }

    }

    public static class Activity4 extends BaseActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Button button = new Button(this);
            button.setText("Activity2_Single_Instance");
            setContentView(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), ActivityTaskTest.class);
                 //   intent.setFlags(Intent.FLAG_ACTIVITY_);
                    startActivity(intent);
                }
            });
        }
    }

    public static class Activity5 extends BaseActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Button button = new Button(this);
            button.setText("Activity5");
            setContentView(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getApplicationContext(), ActivityTaskTest.class));
                }
            });
        }
    }

    public static class Activity6 extends BaseActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Button button = new Button(this);
            button.setText("Activity6");
            setContentView(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getApplicationContext(), ActivityTaskTest.class));
                }
            });
        }
    }

    public static class Activity7 extends BaseActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Button button = new Button(this);
            button.setText("Activity7");
            setContentView(button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getApplicationContext(), ActivityTaskTest.class));
                }
            });
        }
    }
}
